package com.orangelife.service.propFeeBuilding.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeBuilding.IPropFeeBuildingService;
import com.orangelife.service.propFeeBuilding.model.PropFeeBuildingDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeBuilding类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-06-18 12:25:21
 * ==============================================================
 */
@Service("propFeeBuildingService")
public class PropFeeBuildingServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeBuildingService {
	private static Logger logger = Logger
			.getLogger(PropFeeBuildingServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeBuildingServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeBuilding(PropFeeBuildingDto propFeeBuilding)
			throws AppBizException {

		Assert.notNull(propFeeBuilding);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeBuilding.getBuildingName())) {
			if (propFeeBuilding.getBuildingName().length() > 64) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeBuilding.getBuildingNameOther())) {
			if (propFeeBuilding.getBuildingNameOther().length() > 64) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "别名长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeBuilding.getStatus())) {
			if (propFeeBuilding.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeBuilding.getBuildingType())) {
			if (propFeeBuilding.getBuildingType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"楼栋类型 （0单元房  别墅1 暂时不用 ）长度超出");
			}
		}

		if (StringUtils.isBlank(propFeeBuilding.getBuildingID())) {
			propFeeBuilding.setBuildingID(IdUtil.getuuId());
		}
		propFeeBuilding.setItemStat(TypeConst.FLAG_ON);
		propFeeBuilding.setTscreate(new Date());
		propFeeBuilding.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeBuildingMapper.insertSelective", propFeeBuilding);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeBuilding(PropFeeBuildingDto propFeeBuilding)
			throws AppBizException {

		Assert.notNull(propFeeBuilding);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeBuilding.getBuildingID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeBuildingDto oldPropFeeBuilding = this
				.findPropFeeBuildingByID(propFeeBuilding.getBuildingID());

		if (!StringUtils.isBlank(propFeeBuilding.getBuildingName())) {
			if (propFeeBuilding.getBuildingName().length() > 64) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeBuilding.getBuildingNameOther())) {
			if (propFeeBuilding.getBuildingNameOther().length() > 64) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "别名长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeBuilding.getStatus())) {
			if (propFeeBuilding.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeBuilding.getBuildingType())) {
			if (propFeeBuilding.getBuildingType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"楼栋类型 （0单元房  别墅1 暂时不用 ）长度超出");
			}
		}

		if (oldPropFeeBuilding == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeBuilding.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeBuildingMapper.updateByPrimaryKeySelective",
				propFeeBuilding);
	}

	@Override
	public PropFeeBuildingDto findPropFeeBuildingByID(String propFeeBuildingID)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeBuildingID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeBuildingDto oldPropFeeBuilding = this.getSqlSession().selectOne(
				"PropFeeBuildingSearchMapper.selectByPrimaryKey",
				propFeeBuildingID);
		if (oldPropFeeBuilding == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeBuilding.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeBuilding;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeBuildingByLogic(String propFeeBuildingID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeBuildingID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeBuildingDto oldPropFeeBuilding = this
				.findPropFeeBuildingByID(propFeeBuildingID);

		oldPropFeeBuilding.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeBuilding.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeBuildingMapper.updateByPrimaryKeySelective",
				oldPropFeeBuilding);
	}

	@Override
	public List<Map<String, Object>> findPropFeeBuildingByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeBuildingSearchMapper.findAllPropFeeBuilding", map);
		return list;
	}

	@Override
	public List<PropFeeBuildingDto> getPropFeeBuildingByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeBuildingDto> list = this.getSqlSession().selectList(
				"PropFeeBuildingSearchMapper.getAllPropFeeBuildingDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeBuildingByRelevantID(
			String buildingID, String commID, String status, String buildingType)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buildingID", buildingID);
		map.put("commID", commID);
		map.put("status", status);
		map.put("buildingType", buildingType);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeBuildingSearchMapper.selectPropFeeBuildingDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeBuildingDto> findPropFeeBuildingByRelevantID(
			String buildingID, String commID, String status,
			String buildingType, String buildingName) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buildingID", buildingID);
		map.put("commID", commID);
		map.put("status", status);
		map.put("buildingType", buildingType);
		map.put("buildingName", buildingName);
		List<PropFeeBuildingDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeBuildingSearchMapper.findPropFeeBuildingDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeBuildingDto> bauthfindPropFeeBuilding(
			List<String> propFeeBuildingIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeBuildingDto> list = this.getSqlSession().selectList(
				"PropFeeBuildingSearchMapper.batchFindPropFeeBuilding",
				propFeeBuildingIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeBuildingByMap(
			Map<String, Object> propFeeBuilding) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeBuildingSearchMapper.batchFindPropFeeBuildingByMap",
				propFeeBuilding);
		return list;
	}

	@Override
	public int addPropFeeBuildings(List<PropFeeBuildingDto> propFeeBuilding)
			throws AppBizException {

		if (propFeeBuilding != null && propFeeBuilding.size() > 0) {
			return this.getSqlSession().insert(
					"PropFeeBuildingBauthMapper.bauthInsert", propFeeBuilding);
		}
		return 1;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int bauthDeletePropFeeBuilding(List<String> buildingIDs)
			throws AppBizException {
		return this.getSqlSession().update(
				"PropFeeBuildingBauthMapper.bauthDeleteLg", buildingIDs);
	}

}